from fastapi import APIRouter, UploadFile

from app.check import file_check
from app.common.result import Result
from app.config.logging import logger
from app.core import document_manage
from app.models.KnowledgeQueryCondition import KnowledgeQueryCondition

router = APIRouter(prefix="/knowledge", tags=["RAG知识库"])


@router.post(path="/ingest", response_model=Result,
             summary="添加知识文档", description="RAG添加知识文档")
async def knowledge_ingest(file: UploadFile) -> Result:
    """
    添加知识文档到RAG矢量数据库中
    :param file: 知识文件
    :return:
    """
    # 1. 文件参数校验
    check_result = file_check.check_file_requirements(file)
    if not check_result.success:
        return check_result
    # 2. 知识文档处理
    return document_manage.process(file)


@router.post(path="/search", summary="知识检索", description="检索知识从RAG中")
async def knowledge_search(knowledge_query_condition: KnowledgeQueryCondition):
    """
    知识检索
    :return: Result
    """
    logger.info(f"knowledge_search query: {knowledge_query_condition.model_dump_json()}")
    return document_manage.search(knowledge_query_condition)


@router.delete(path="/delete", summary="删除知识文档", description="删除RAG知识库内容")
async def knowledge_delete():
    """
    删除RAG知识库内容
    :return:
    """
    return None
